package com.syh.jn.scm.service.impl.supplier.base.downstream;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import cn.redscarf.commons.util.other.ExtAssert;
import cn.redscarf.commons.wrapper.Wrapper;
import com.alibaba.dubbo.config.annotation.Service;
import com.syh.core.ddd.infrastructure.repository.AggregateRepository;
import com.syh.core.ddd.service.AbstractServiceImpl;
import com.syh.jn.scm.domain.supplier.entity.base.downstream.DownstreamPriceTemplate;
import com.syh.jn.scm.domain.supplier.entity.base.downstream.DownstreamPriceTmplDetail;
import com.syh.jn.scm.domain.supplier.repository.base.downstream.DownstreamPriceTemplateRepository;
import com.syh.jn.scm.dto.supplier.base.downstream.DownstreamPriceTemplateMto;
import com.syh.jn.scm.dto.supplier.base.downstream.DownstreamPriceTemplateSto;
import com.syh.jn.scm.dto.supplier.base.downstream.DownstreamPriceTemplateVo;
import com.syh.jn.scm.dto.supplier.base.downstreamPriceTmplDetail.DownstreamPriceTmplDetailMto;
import com.syh.jn.scm.dto.supplier.base.downstreamPriceTmplDetail.DownstreamPriceTmplDetailSto;
import com.syh.jn.scm.dto.supplier.base.downstreamPriceTmplDetail.DownstreamPriceTmplDetailVo;
import com.syh.jn.scm.exception.BusinessException;
import com.syh.jn.scm.service.api.supplier.base.downstream.DownstreamPriceTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 客户报价模板
 */
@Service
@Transactional
public class DownstreamPriceTemplateServiceImpl extends AbstractServiceImpl<DownstreamPriceTemplateSto, DownstreamPriceTemplateMto, DownstreamPriceTemplateVo, DownstreamPriceTemplate, Long> implements DownstreamPriceTemplateService {

  @Autowired
  private DownstreamPriceTemplateRepository downstreamPriceTemplateRepository;

  @Override
  protected AggregateRepository<DownstreamPriceTemplate, Long> getAggregateRepository() {
    return downstreamPriceTemplateRepository;
  }

  /**
   * 新建客户报价模板
   *
   * @param sto
   * @return
   */
  @Override
  public Long save(DownstreamPriceTemplateSto sto) {
    DownstreamPriceTemplate goodsPriceTemplate = new DownstreamPriceTemplate(sto);
    return downstreamPriceTemplateRepository.add(goodsPriceTemplate);
  }

  /**
   * 修改客户报价模板
   *
   * @param mto
   */
  @Override
  public void update(DownstreamPriceTemplateMto mto) {
    DownstreamPriceTemplate goodsPriceTemplate = downstreamPriceTemplateRepository.getById(mto.getId());
    goodsPriceTemplate.modify(mto);
    downstreamPriceTemplateRepository.update(goodsPriceTemplate);
  }

  /**
   * 删除
   *
   * @param id
   */
  @Override
  public void delete(Long id) {
    DownstreamPriceTemplate goodsPriceTemplate = downstreamPriceTemplateRepository.getById(id);
    goodsPriceTemplate.destroy();
    downstreamPriceTemplateRepository.remove(goodsPriceTemplate);
  }

  /**
   * 启用
   *
   * @param id
   */
  @Override
  public void enabled(Long id) {
    DownstreamPriceTemplate goodsPriceTemplate = downstreamPriceTemplateRepository.getById(id);
    goodsPriceTemplate.enabled();
    downstreamPriceTemplateRepository.update(goodsPriceTemplate);
  }

  /**
   * 停用
   *
   * @param id
   */
  @Override
  public void disabled(Long id) {
    DownstreamPriceTemplate goodsPriceTemplate = downstreamPriceTemplateRepository.getById(id);
    goodsPriceTemplate.disabled();
    downstreamPriceTemplateRepository.update(goodsPriceTemplate);
  }

  /**
   * 批量保存客户报价商品
   *
   * @param stoList
   */
  @Override
  public void batchSaveDownstreamPriceTmplDetail(List<DownstreamPriceTmplDetailSto> stoList) {
    List<DownstreamPriceTmplDetail> details = new ArrayList<>();
    stoList.stream().forEach((sto)->{
      DownstreamPriceTmplDetail downstreamPriceTmplDetail = new DownstreamPriceTmplDetail(sto);
      details.add(downstreamPriceTmplDetail);
    });
    downstreamPriceTemplateRepository.batchSaveDownstreamPriceTmplDetail(details);
  }

  /**
   * 批量修改客户报价商品
   *
   * @param mtoList
   */
  @Override
  public void batchUpdateDownstreamPriceTmplDetail(List<DownstreamPriceTmplDetailMto> mtoList) {
    List<DownstreamPriceTmplDetail> details = new ArrayList<>();
    mtoList.stream().forEach((mto)->{
      DownstreamPriceTmplDetail downstreamPriceTmplDetail = downstreamPriceTemplateRepository.getByQuotationId(mto.getId());
      downstreamPriceTmplDetail.modify(mto);
      details.add(downstreamPriceTmplDetail);
    });
    downstreamPriceTemplateRepository.batchUpdateDownstreamPriceTmplDetail(details);
  }

  /**
   * 删除客户报价商品
   *
   * @param id
   */
  @Override
  public void deleteDownstreamPriceTmplDetail(Long id) {
    DownstreamPriceTmplDetail detail = downstreamPriceTemplateRepository.getByQuotationId(id);
    detail.destroy();
    downstreamPriceTemplateRepository.deleteDownstreamPriceTmplDetail(detail);
  }

  /**
   * 根据条件查询客户报价列表
   *
   * @param wrapper
   * @param startRow
   * @param count
   * @return
   */
  @Override
  public List<DownstreamPriceTmplDetailVo> findQuotationListPage(Wrapper wrapper, Integer startRow, Integer count) {
    List<DownstreamPriceTmplDetail> details = downstreamPriceTemplateRepository.findQuotationListPage(wrapper, startRow, count);
    return this.transform(details);
  }

  /**
   * 根据商品id拿商品模板
   *
   * @param id
   * @return
   */
  @Override
  public Boolean getByGoodsId(Long id,Long tepId) {
    ExtAssert.notNull(id, "id不能为空");
    Boolean flag = downstreamPriceTemplateRepository.getByGoodsId(id, tepId);
    if(!flag){
      throw new BusinessException("该商品已经在该模板中,请选择另外一个");
    }
    return flag;
  }

  /**
   * 领域对象转vo
   *
   * @param entityList
   * @return
   */
  private List<DownstreamPriceTmplDetailVo> transform(List<DownstreamPriceTmplDetail> entityList) {
    List<DownstreamPriceTmplDetailVo> voList = new ArrayList<>();
    entityList.stream().forEach((entity)->{
      DownstreamPriceTmplDetailVo detailVo = new DownstreamPriceTmplDetailVo();
      ExtBeanUtils.copyProperties(entity, detailVo);
      voList.add(detailVo);
    });
    return voList;
  }
}
